﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using cde.UserControl;
using System.Collections.Specialized;
using System.Text;
using Cit.CDE.Net.Module;
using cde.cache;
using Cit.CDE.Net.Module.Commons;
using cde.SystemCommon;
using System.Text.RegularExpressions;

public partial class main_comm_SearchCandidate : BasePage
{
    private const string EM_FN = "EM_FN";
    private const string TARGET_NAME = "TARGET_NAME";
    private const string TARGET_OBJ = "TARGET_OBJ";
    private const string CLM_SIZE = "CLM_SIZE";

    private CDEEntityManager moto_em;
    private List<string> attNames;
    private CDESearchCriteria sc;
    private DataTable dt;
    private Hashtable attribute_value_name;

    protected void Page_Load(object sender, EventArgs e)
    {
        string moto_EM_FN = Request.QueryString[EM_FN];
        moto_em = CDEFactory.getEMByFullName(moto_EM_FN);

        string targetAttName = Request.QueryString[TARGET_NAME];
        targetIds.Value = Request.QueryString[TARGET_OBJ];
        string[] names = targetAttName.Split(new char[] { ',' });
        attNames = new List<string>(names);

        int clmSize = 2;
        string strSize = Request.QueryString[CLM_SIZE];
        if (!CDEConstants.IsEmpty(clmSize))
        {
            clmSize = int.Parse(strSize);
        }

        //sc = SearchCriteriaWithXml.CreateSC(moto_em, (Oprator)Session[CDEConstants.SESSION_LOIN_OPRATOR]);
        sc = new CDESearchCriteria(moto_em);
        sc.setSystemDefaultValue((Oprator)Session[CDEConstants.SESSION_LOIN_OPRATOR]); ;
        SearchDataGrid.setScFromRequest(moto_em, clmSize, sc, Request.QueryString);
        sc.setHiddenDefaultValue(moto_em, (Oprator)Session[CDEConstants.SESSION_LOIN_OPRATOR]);
        foreach (string name in Request.QueryString.Keys)
        {
            if (Regex.IsMatch(name, "^(HID_).+(_SET)$"))
            {
                string value = Request.QueryString[name];

                sc.addCondition(name.Substring(4, name.Length - 8), value.Substring(0, value.LastIndexOf("_")), (CDEConstCompare)int.Parse(value.Substring(value.LastIndexOf("_") + 1)));
            }
        }

        StringBuilder sbr = new StringBuilder();
        sbr.AppendFormat(Resources.Resource.ERR_MSG_OVERSTEP, sc.PageSize);
        lblWanring.Text = sbr.ToString();

        attribute_value_name = new Hashtable();
        dt = (DataTable)Session["datatable"];
        addDgColumns(moto_em, attNames);

        if (!Page.IsPostBack)
        {
            SearchToTable();
            Session["datatable"] = dt;
        }

        dgmeisai.DataSource = dt;
        dgmeisai.DataBind();
    }

    private void SearchToTable() 
    {
        DataTable searchCandidatesDt = CDEService.LoadSearchCandidates(moto_em, sc, attNames);

        lblResultCnt.Text = "" + searchCandidatesDt.Rows.Count;
        dt.Rows.Clear();
        foreach (DataRow sdr in searchCandidatesDt.Rows)
        {
            DataRow dr = dt.NewRow();
            string values = "";
            foreach (string attributeName in attNames)
            {
                values += sdr[attributeName] + ",";
            }

            foreach (string attributeName in attNames)
            {
                string disp_value = this.getDisply(attributeName, EntityWithXml.getObject2String(sdr[attributeName]));

                dr[attributeName] = "<a href=\"JavaScript:set_openerValue('" + values + "')\">" + disp_value + "</a>";
            }

            dt.Rows.Add(dr);
        }
    }

    private string getDisply(string attributeName, string value)
    {
        if (attribute_value_name.Contains(attributeName))
        {
            Hashtable value_name = (Hashtable)attribute_value_name[attributeName];
            if (value_name.Contains(value))
            {
                return (string)value_name[value];
            }
        }

        return value;
    }

    private void addDgColumns(CDEEntityManager em, List<string> attNames)
    {
        if (dt == null)
        {
            dt = new DataTable("entities");
        }

        foreach (string attributeName in attNames)
        {
            BoundColumn bc = new BoundColumn();
            string label = "";
            string clm_type = "";
            CDEColumn pclm;

            CDEAttribute dispA = em.getAttribute(attributeName);
            if (dispA != null)
            {
                label = dispA.Label;
                pclm = dispA.getP_Colmun();
            }
            else
            {
                CDECondition dispC = em.getCondition(attributeName);
                label = dispC.Label;
                pclm = dispC.getP_Colmun();
            }
            clm_type = pclm.Type;

            bc.HeaderText = label + "<br /><input type=\"text\" name=\"" + attributeName + "\" style=\"width:98%;\">";
            bc.DataField = attributeName;
            if (!dt.Columns.Contains(attributeName))
            {
                dt.Columns.Add(attributeName);
            }

            if (clm_type == "S")
            {
                bc.ItemStyle.HorizontalAlign = HorizontalAlign.Left;
            }
            else if (clm_type == "N")
            {
                bc.ItemStyle.HorizontalAlign = HorizontalAlign.Right;
            }

            dgmeisai.Columns.Add(bc);

            if (pclm.isHaveCandidate())
            {
                List<CDECandidate> listCandidate = pclm.getCandidates();
                Hashtable value_name = new Hashtable();
                foreach (CDECandidate candidate in listCandidate)
                {
                    value_name.Add(candidate.Value, candidate.Label);
                }
                attribute_value_name.Add(attributeName, value_name);
            }
        }

    }

    protected void dgmeisai_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
        dgmeisai.CurrentPageIndex = e.NewPageIndex;
        Page.DataBind();
    }
    protected void cmdSearch_Click(object sender, EventArgs e)
    {
        dgmeisai.CurrentPageIndex = 0;
        foreach (string attributeName in attNames)
        {
            string value = Request.Form[attributeName];
            if (!CDEConstants.IsEmpty(value))
            {
                sc.addCondition(attributeName, value);
            }
        }
        SearchToTable();
        dgmeisai.DataSource = dt;
        dgmeisai.DataBind();
        Session["datatable"] = dt;
    }
}
